package com.ecb.business.service.impl;

import com.ecb.business.dto.api.ChainMakerRemoteResult;
import com.ecb.business.dto.chain.QueryChainDTO;
import com.ecb.business.dto.chainExec.ChainQueryDTO;
import com.ecb.business.dto.chainExec.SubscribeTradeDTO;
import com.ecb.business.exception.BusinessException;
import com.ecb.business.po.ChainConfigPO;
import com.ecb.business.remote.chainmaker.ChainMakerRemote;
import com.ecb.business.service.ChainConfigManageService;
import com.ecb.business.service.ChainExecService;
import com.ecb.business.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ChainExecServiceImpl implements ChainExecService {

    @Value("${chainmaker.url:127.0.0.1}")
    public String chainMakerUrl;
    @Autowired
    ChainConfigManageService chainConfigManageService;
    @Autowired
    ChainMakerRemote chainMakerRemote;
    @Override
    public Map getChainDetail(ChainQueryDTO queryDTO) {
        QueryChainDTO queryChainDTO = new QueryChainDTO();
        queryChainDTO.setId(queryChainDTO.getId());
        ChainConfigPO chainConfigById = chainConfigManageService.getChainConfigById(queryChainDTO);
        Map<String, String > body = new HashMap<>();
        body.put("ChainId", chainConfigById.getChainId());
        String getChainDetail = chainMakerRemote.getChainDetail(chainMakerUrl, "getChainDetail", queryDTO.getCmToken(), body);
        ChainMakerRemoteResult parse = chainMakerRemote.parse(getChainDetail);
        if (!parse.isOk()){
            throw new BusinessException("500","调用接口获取链信息失败");
        }
        return JsonUtils.beanToBean(parse, HashMap.class);
    }

    @Override
    public boolean subscribeTrade(SubscribeTradeDTO subscribeTradeDTO) {
        QueryChainDTO queryChainDTO = new QueryChainDTO();
        queryChainDTO.setId(queryChainDTO.getId());
        ChainConfigPO chainConfigById = chainConfigManageService.getChainConfigById(queryChainDTO);
        Map<String, String > body = new HashMap<>();
        body.put("ChainId", chainConfigById.getChainId());
//        String getChainDetail = chainMakerRemote.getChainDetail(chainMakerUrl, "getChainDetail", queryDTO.getCmToken(), body);
//        ChainMakerRemoteResult parse = chainMakerRemote.parse(getChainDetail);
//        if (!parse.isOk()){
//            throw new BusinessException("500","调用接口获取链信息失败");
//        }
        return false;
    }
}
